package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.common.entity.Result;
import com.itheima.mapper.CheckGroupMapper;
import com.itheima.mapper.SetmealMapper;
import com.itheima.pojo.CheckGroup;
import com.itheima.service.CheckGroupService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class CheckGroupServiceImpl implements CheckGroupService {

    @Autowired
    private CheckGroupMapper checkGroupMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 新增检查组
     * @param group
     * @return
     */
    @Transactional
    public void add(CheckGroup group,Integer[] checkitemIds) {
        checkGroupMapper.insert(group);
        Integer gruopId = group.getId();
        if (checkitemIds != null && checkitemIds.length > 0){
            for (Integer checkitemId : checkitemIds) {
                checkGroupMapper.insertCheckitemIds(gruopId,checkitemId);
            }
        }
    }


    /**
     * 删除检查组
     * @param id
     * @return
     */
    @Transactional
    public void delete(Integer id) {
        //删除检查组
        checkGroupMapper.delete(id);
        //删除检查组和检查项的关联关系
        checkGroupMapper.deleteAssociation(id);
    }

    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */

    /**
     * 编辑检查组
     * @param group
     * @return
     */
    @Transactional
    public void edit(CheckGroup group, Integer[] checkitemIds) {
        log.info("编辑检查组:{}",group,checkitemIds);
        checkGroupMapper.edit(group,checkitemIds);
        Integer groupId = group.getId();
        if (checkitemIds != null && checkitemIds.length > 0){
            for (Integer checkitemId : checkitemIds) {
                checkGroupMapper.updateCheckitemIds(groupId,checkitemId);
            }
        }
    }

    /**
     * 根据id查询检查组
     */
    public CheckGroup findById(Integer id) {
        CheckGroup checkGroup = checkGroupMapper.findById(id);
        return checkGroup;
    }

    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    public PageResult page(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        Page<CheckGroup> page = checkGroupMapper.selectByCondition(queryPageBean);
        PageResult pageResult = new PageResult(page.getTotal(),page.getResult());
        return pageResult;
    }

    /**
     * 查询所有检查组
     */
    public List<CheckGroup> findAll() {
        List<CheckGroup> CheckGroup = checkGroupMapper.selectall();
        return CheckGroup;
    }

    @Override
    public List<Integer> findCheckGroupIdBysetmealId(Integer setmealId) {
        List<Integer> groupIds = checkGroupMapper.findCheckGroupIdBysetmealId(setmealId);
        return groupIds;
    }

    @Override
    public List<CheckGroup> findCheckGroupBysetmealId(Integer mealId) {
        List<CheckGroup> checkGroups = setmealMapper.findCheckGroupBysetmealId(mealId);
        return checkGroups;
    }
}
